﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Plat.CRM.Customers.Interface;
using Microhard.Plat.CRM.Customers.DataAccess;
using Microhard.Plat.CRM.Customers.DTO;
using Microhard.Plat.CRM.Customers.Msg;
using Microhard.Plat.CRM.Customers.Models;

namespace Microhard.Plat.CRM.Customers.Impl
{
    public class CustomerImpl : ICustomer
    {
        public Microhard.Core.DBUtility.ExecutedResult Add(Models.CustomerModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            var op = model.ToEntity();
            //openid是否已经注册过
            var cus = db.Customers.SingleOrDefault(o => o.OpenID == model.OpenID);
            if (cus == null)
            {
                //微信自己注册的会员，默认跟进人员为空
                if (model.SaleEmployeeID == Guid.Empty)
                {
                    op.Mark += "微信注册，无销售跟进";
                }
                db.Customers.AddObject(op);
            }
            else
            {
                cus.headimgurl = model.headimgurl;
                cus.NickName = model.NickName;
                cus.City = model.City;
                cus.Province = model.Province;
                cus.Country = model.Country;
                cus.CreateTime = DateTime.Now;
                model.CustomerID = cus.CustomerID;
            }
            try
            {

                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, CustomerMsg.AddFailed);
                Log.Error("ICustomer.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.CustomerModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.CustomerModel model)
        {
            throw new NotImplementedException();
        }

        public Models.CustomerModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.CustomerModel GetModelByModel(Models.CustomerModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.CustomerModel> Query(Models.CustomerModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            totalCount = 0;
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();

            IEnumerable<Customer> entities = null;
            if (!string.IsNullOrEmpty(Model.Cellphone))
            {
                entities = from b in db.Customers
                           where b.Cellphone == Model.Cellphone
                           && b.CompanyID == this.scope.EmpInfo.CompanyID
                           select b;
            }
            else
            {
                entities = from b in db.Customers
                           where b.CompanyID == this.scope.EmpInfo.CompanyID
                           select b;
            }
            if (entities != null)
            {
                totalCount = entities.Count();
                var pagedEntities = entities.OrderBy(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                if (pagedEntities != null && pagedEntities.Count > 0)
                    return pagedEntities.Select(m => m.ToModel()).ToList();
            }
            return new List<CustomerModel>();
        }

        public List<Models.CustomerModel> Query(Models.CustomerModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public bool IsExist(CustomerModel Model)
        {
            //微信用户注册且填写了销售线索，才能算注册会员
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            bool result = db.Customers.Count(c => c.OpenID == Model.OpenID && c.SaleLeads.Count > 0) > 0 ? true : false;
            if (!result)
            {
                result = db.SaleLeads.Count(s => s.CustomerID == Model.CustomerID) > 0 ? true : false;
            }
            return result;
        }
    }
}
